import { useRoutes, RouteObject } from "react-router-dom"
import { createLogger } from "@/utils/logger"
import { memo } from "react"

const logger = createLogger("SubRouter")

type SubRouterProps = {
    routes: RouteObject[]
    rootElement: React.ReactNode
}

/**
 * 路由組件
 * 處理路由配置和渲染
 */
function SubRouter_(props: SubRouterProps) {
    logger.debug("routes", props.routes)

    // 使用 React Router 的 useRoutes hook 創建路由
    const element = useRoutes([
        {
            path: "/",
            element: props.rootElement,
            children: props.routes,
        },
    ])
    return element
}

const SubRouter = memo(SubRouter_)

export default SubRouter
